package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Company", "/company", "/CompanyServlet",
		"/companyServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public CompanyServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<CompanyModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("companyNew.jsp")
						.forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			CompanyModel query = new CompanyModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModel());

			CompanyModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("obj", obj);
				getServletContext().getRequestDispatcher("companyEdit.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			CompanyModel query = new CompanyModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModel());

			CompanyModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("obj", obj);
				getServletContext().getRequestDispatcher("companyView.jsp")
						.forward(request, response);

			} else {
				// TODO : data not found
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			CompanyModel obj = new CompanyModel();

			String is_competitor = request.getParameter("is_competitor");
			obj.setIsCompetitor(is_competitor.equalsIgnoreCase("true")
					|| is_competitor.equalsIgnoreCase("1"));

			String abb_name = request.getParameter("abb_name");
			obj.setAbbName(abb_name);

			String short_name = request.getParameter("short_name");
			obj.setShortName(short_name);

			String full_name = request.getParameter("full_name");
			obj.setFullName(full_name);

			String foreign_name = request.getParameter("foreign_name");
			obj.setForeignName(foreign_name);

			String address = request.getParameter("address");
			obj.setAddress(address);

			String mobile = request.getParameter("mobile");
			obj.setMobile(mobile);

			String tax_code = request.getParameter("tax_code");
			obj.setTaxCode(tax_code);

			String description = request.getParameter("description");
			obj.setDescription(description);

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			String is_draft = request.getParameter("is_draft");
			obj.setIsDraft(is_draft.equalsIgnoreCase("true")
					|| is_draft.equalsIgnoreCase("1"));

			String notification = request.getParameter("notification");
			if (notification != null && !notification.isEmpty()) {
				obj.setNotification(Integer.parseInt(notification));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
						new CompanyModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					response.sendRedirect("companyNew.jsp");
				} else {
					// TODO : processing error here

				}
			} else {
				// invalid input

				request.setAttribute("company", obj);
				getServletContext().getRequestDispatcher("/companyInput.jsp")
						.forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			CompanyModel query = new CompanyModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModel());

			CompanyModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String is_competitor = request.getParameter("is_competitor");
				obj.setIsCompetitor(is_competitor.equalsIgnoreCase("true")
						|| is_competitor.equalsIgnoreCase("1"));

				String abb_name = request.getParameter("abb_name");
				obj.setAbbName(abb_name);

				String short_name = request.getParameter("short_name");
				obj.setShortName(short_name);

				String full_name = request.getParameter("full_name");
				obj.setFullName(full_name);

				String foreign_name = request.getParameter("foreign_name");
				obj.setForeignName(foreign_name);

				String address = request.getParameter("address");
				obj.setAddress(address);

				String mobile = request.getParameter("mobile");
				obj.setMobile(mobile);

				String tax_code = request.getParameter("tax_code");
				obj.setTaxCode(tax_code);

				String description = request.getParameter("description");
				obj.setDescription(description);

				String status = request.getParameter("status");
				obj.setStatus(Integer.parseInt(status));

				String is_draft = request.getParameter("is_draft");
				obj.setIsDraft(is_draft.equalsIgnoreCase("true")
						|| is_draft.equalsIgnoreCase("1"));

				String notification = request.getParameter("notification");
				obj.setNotification(Integer.parseInt(notification));

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("company", obj);
					getServletContext()
							.getRequestDispatcher("/companyEdit.jsp").forward(
									request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			CompanyModel query = new CompanyModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<CompanyModel, CompanyModelExt> dal = new GenericDal<CompanyModel, CompanyModelExt>(
					new CompanyModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
			} else {
				// TODO fail
			}
		}
	}

}